package cn.com.pansky.service;

import java.sql.Timestamp;

import com.alibaba.fastjson.JSONObject;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import cn.com.pansky.util.EncryptionUtils;
import cn.com.pansky.util.ProtectUtil;

public class UserService extends CommonAction {
	
	CommonService cs = new CommonService();
	
	public void autoLogin(String token) {

		String[] str = token.split("_");
		String userId = str[0];
		String lastLoginTime = str[1];
		String key = str[2];
		String sql = "SELECT b.AA002 from rc10 a , ra02 b  WHERE a.aa001 = b.aa001 AND  a.aa001 = ? AND aa005 = '0'";
		Model m = SqlHelper.queryOne(sql, userId);
		if (m != null) {
			String AA002 = m.getString("AA002");
			String key_ = EncryptionUtils.md5(userId + AA002 + lastLoginTime);
			if (key.equals(key_)) {
				String findUser = "SELECT R1.CA003, R1.CA006, R2.AA001, R2.AA032, R2.AA111 FROM RC03 R1 "
						+ "JOIN (SELECT AA032, AA001, CA001, AA111 FROM RC10 WHERE AA001 = ?) R2 "
						+ "ON R1.CA001 = R2.CA001";
				Model m_ = SqlHelper.queryOne(findUser, userId);
				m_.put("CA006", ProtectUtil.help1(m_.getString("CA006")));
				context.getServletSession().setAttribute("user", m_);// session中保存用户信息
				context.getDatas().put("result", m_);
			} else {
				context.putError(1, "自动登录失败，请重新登录");
			}
		} else {
			context.putError(1, "自动登录失败，请重新登录");
		}
	}

	/**
	 * 动态验证码登录发送短信验证码
	 * 
	 * @param AA032
	 */
	public void sendLoginCode(String AA032, String type) {
		String sql = "SELECT AA001, AAE016 FROM RC10 WHERE AA032 = ? and AA003 = ?";
		// 根据身份证获取用户信息
		Model m = SqlHelper.queryOne(sql, AA032, type);
		if (m != null) {
			if("0".equals(type)) {
				send(AA032);
				return;
			}
			String AAE016 = m.getString("AAE016");
			switch (Integer.parseInt(AAE016)) {
			case 0:
				datas.put("isPass", false);
				datas.put("state", 0);
				datas.put("info", "账户未审核！");
				break;
			case 2:
				datas.put("isPass", false);
				datas.put("state", 2);
				datas.put("info", "账户审核未通过！");
				break;

			default:
				
				try {
					JSONObject obj = cs.aliyunSms(AA032);
					if (!"0".equals(obj.getString("code"))) {
						JSONObject json = new JSONObject();
						json.put("isPass", false);
						json.put("info", obj.getString("msg"));
						context.getDatas().put("result", json);
						return;
					} else {
						JSONObject json = new JSONObject();
						json.put("isPass", true);
						context.getDatas().put("result", json);
						context.getServletSession().setAttribute("AA032", AA032);
						context.getServletSession().setAttribute("AF016", obj.getString("code"));
						return;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		} else {
			datas.put("isPass", false);
			datas.put("info", "账户不存在，请注册后再登录");
		}
		context.getDatas().put("result", datas);
	}

	/**
	 * 发送短信验证码
	 * @param AA032
	 */
	private void send(String AA032) {
		
		CommonService cs = new CommonService();
		JSONObject obj;
		String code = "";
		try {
			obj = cs.aliyunSms(AA032);
			code = obj.getString("content");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		context.getServletSession().setAttribute("AA032", AA032);
		context.getServletSession().setAttribute("AF016", code);
		datas.put("isPass", true);
		
		context.getDatas().put("result", datas);
	}

	/**
	 * 校验验证码
	 * 
	 * @throws Exception
	 */
	public void validateCode() {
		if (context.getSessionVal("AA032")!=null && context.getSessionVal("AF016")!=null) {
			Model m = SqlHelper.queryOne("select * from (select * from CA20 where AA032 = ? and AF016 = ? order by AF014 desc) where rownum = 1", 
					context.getSessionVal("AA032"),getParam("AF016"));
			//当前时间
			long nowTime = new Timestamp(System.currentTimeMillis()).getTime();
			if (m != null) {
				//验证码发送时间
				long AF017 = m.getLong("AF017");
				double time = (nowTime - AF017)/1000/60;
				if (time>5) {
					datas.put("isPass", false);
					datas.put("info", "验证码超时");
				}else{
					context.getServletSession().removeAttribute("AF016");
				}
			} else {
				datas.put("isPass", false);
				datas.put("info", "验证码输入错误");
			}
		}else{
			datas.put("isPass", false);
			datas.put("info", "验证码已失效，请重新获取");
		}
	}
	
	/**
	 * 保存密码信息
	 * @param userId
	 */
	public void addPass(String userId) {
		String pass = EncryptionUtils.md5(getParam("AA002")+userId);
		String savePass = urlConfig.getVar("savePass"); // 获取sql
		SqlHelper.execute(savePass, userId, pass);
	}

	
	public static void main(String[] args) {
	}
}
